<template>
  <div class="login-body">
    <div class="login-panel">
      <div class="login-title">用户登录</div>
      <el-form :model="formData" :rules="rules" ref="formDataRef">
        <el-form-item prop="mobile">
          <el-input
            placeholder="请输入账号"
            v-model="formData.mobile"
            size="large"
            type="text"
          >
            <template #prefix>
              <el-icon>
                <User />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="formData.password"
            size="large"
            type="password"
            @keyup.enter.native="login()"
          >
            <template #prefix>
              <el-icon>
                <Lock />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <!-- 验证码 -->
        <el-form-item prop="code">
          <el-row :gutter="5" style="width: 100%;">
            <el-col :span="18">
              <el-input
                style="width: 100%"
                placeholder="请输入验证码"
                v-model="formData.code"
                size="large"
                type="text"
              >
              </el-input>
            </el-col>
            <el-col :span="6">
              <el-image @click="getCode()" style="width: 100%; height: 40px" :src="codeInfo.captcha" fit="cover" />
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="">
          <el-button
            type="primary"
            style="width: 100%"
            @click="loginFn()"
            size="large"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import {login, captcha} from "@/api/login";
import { debounce } from 'lodash';

//表单
const formDataRef = ref();
const codeInfo = reactive({
  captcha: "",
  captchaId: "",
})
let formData = reactive({
  mobile: "",
  password: "",
  type:1
});
const rules = {
  mobile: [
    {
      required: true,
      message: "请输入用户名",
    },
  ],
  password: [
    {
      required: true,
      message: "请输入密码",
    },
  ],
  code: [
    {
      required: true,
      message: "请输入验证码",
    },
  ],
};

const router = useRouter();

const loginFn = () => {
  var form_obj = JSON.parse(JSON.stringify(formData));
  form_obj.captchaId = codeInfo.captchaId;
  formDataRef.value.validate((valid) => {
    if (valid) {
      login(form_obj).then((res) => {
        console.log(res);
        if (res.code==0) {
          ElMessage({
            message: "登录成功",
            type: "success",
          });
          window.localStorage.setItem("yxUserInfo", JSON.stringify(res.data));
          window.localStorage.setItem("yxUserName", formData.mobile);
          router.push("/");
        } else {
          ElMessage.error(res.msg ||  "账号或密码错误！");
          getCodeImage()
        }
      });
    }
  })
};
const getCode = debounce(async () => {
  getCodeImage();
}, 1000); // 300ms 是防抖时间，可以根据需要调整


const getCodeImage = async () => {
  const res = await captcha();
  if (res.code == 0) {
    codeInfo.captcha = res.data.captcha;
    codeInfo.captchaId = res.data.captchaId;
  } else {
    ElMessage.error(res.msg || "获取验证码失败！");
  }
};

onMounted(() => {
  getCodeImage()
});
</script>

<style lang="less" scoped >
.login-body {
  background-image: linear-gradient(120deg, #e0c3fc 0%, #8ec5fc 100%);
  height: 100%;
  width: 100%;
  background-size: cover;
  position: absolute;
  left: 0;
  top: 0;

  .login-panel {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;

    padding: 25px;
    width: 26%;
    min-width: 460px;
    height: 40%;
    min-height: 300px;
    background: rgba(255, 255, 255, 0.6);
    border-radius: 5%;
    box-shadow: 2px 2px 10px #ddd;

    .login-title {
      font-size: 22px;
      text-align: center;
      margin-bottom: 22px;
    }
  }
}
</style>
